package com.learn.ert.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.ert.base.BaseApiService;
import com.learn.ert.base.BaseResponse;
import com.learn.ert.mapper.ErEmpMapper;
import com.learn.ert.pojo.ErEmpEntity;
import com.learn.ert.service.ErEmpService;
import com.learn.ert.utils.ProductToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 员工登录
 */
@RestController
@CrossOrigin
@Api(tags = "用户登陆接口")
public class LoginController extends BaseApiService<JSONObject> {

    @Autowired
    ErEmpService erEmpService;

    @Autowired
    private ProductToken productToken;

    @GetMapping("/userLogin")
    @ApiOperation(value = "登陆接口", notes = "用户登陆")
    @ApiImplicitParams({@ApiImplicitParam(name = "eNumber", value = "账号", required = true,
            dataType = "String"),@ApiImplicitParam(name = "ePassword", value = "密码", required = true,
            dataType = "String")})
    public BaseResponse<JSONObject> userLogin(String eNumber, String ePassword, HttpServletRequest request) {
        QueryWrapper<ErEmpEntity> queryWrapper = new QueryWrapper<ErEmpEntity>();
        System.out.println(eNumber+"eNumber");
        System.out.println(ePassword+"ePassword");
        //账号验证
        queryWrapper.eq(StringUtils.isNoneEmpty(eNumber),"e_number",eNumber);
        //密码验证
        queryWrapper.eq(StringUtils.isNoneEmpty(ePassword),"e_password",ePassword);

        //1.校验登陆是否成功
        ErEmpEntity empEntity = erEmpService.getOne(queryWrapper);
        //2.如果不成功返回提示
        if (empEntity == null) {
            return setResultError("账户名密码错误！！！");
        } else {
            //3.如果成功，生产一个token
            String token = UUID.randomUUID().toString().replaceAll("-", "");
            System.out.println(token+"token");
            System.out.println(empEntity.getEId().toString()+"empEntity.getEId().toString()");
            /*
             * 一开始采用的是这样的增加到map中但是map获得值得key和value是空，换了一种方式
             * Map<String, String> mapInfo = productToken.productToken(user.getId().toString(), token);
             *
             * */
            System.out.println("登錄了一次新增一次數據");
            Map<String, String> objInfo = new HashMap<String, String>();
            productToken.productToken(empEntity.getEId().toString(), token);
            objInfo.put("adminid",empEntity.getEId().toString());
            objInfo.put("token",token);


            //3.返回token信息（有效期50分钟）
//            return setResultSuccess((Object)objInfo);
            return setResultSuccess((JSONObject) JSONObject.toJSON(objInfo));

        }
    }

    // 手机号userEntityList
    /*List<ErEmpEntity> userEntityList;*/

    /**
     * 缓存所有手机号，前端每点击3次请求一回
     * @return
     */
    /*@PostMapping("/allPhone")
    public List<ErEmpEntity> allPhone(){
        userEntityList= erEmpMapper.selectList(null);
        System.out.println("11");
        return userEntityList;
    }*/

    /**
     * 获得手机号验证是否存在，存在可以获取验证码
     * @param request
     * @return
     */
   /* @PostMapping("/phoneShow")
    @ApiOperation(value = "登录验证手机号", notes = "用户登陆")
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号码", required = true,
            dataType = "String")})
    public BaseResponse<JSONObject> phoneShow(HttpServletRequest request){
        boolean kep=false;
        String phone = request.getParameter("phone");//手机号
        for (ErEmpEntity e:userEntityList) {
            if (e.getEPhone().equals(phone)){
                kep = true;
                break;
            }else {
                kep=false;
            }
        }
        return kep?setResultSuccess("手机号正确可以获得验证码登录"):setResultError("手机号错误");
    }*/

    /**
     * 登陆接口,账号和密码
     * @param erEmpEntity
     * @return
     */
    /*@PostMapping("/login")
    @ApiOperation(value = "登陆接口", notes = "用户登陆")
    @ApiImplicitParams({@ApiImplicitParam(name = "eAccount", value = "账号", required = true,
            dataType = "String"), @ApiImplicitParam(name = "ePassword", value = "密码", required = true,
            dataType = "String")})
    public BaseResponse<JSONObject> login(ErEmpEntity erEmpEntity) {
        QueryWrapper<ErEmpEntity> queryWrapper = new QueryWrapper<ErEmpEntity>();
        //账号验证
        queryWrapper.eq(StringUtils.isNoneEmpty(erEmpEntity.getEAccount()),"e_account",erEmpEntity.getEAccount());
        //密码验证
        queryWrapper.eq(StringUtils.isNoneEmpty(erEmpEntity.getEPassword()),"e_password",erEmpEntity.getEPassword());

        return erEmpMapper.selectOne(queryWrapper)!=null?setResultSuccess("登陆成功"):setResultError("登陆失败");
    }*/

}
